<template>
  <div class="content_wrap">
    <Header />
    <div class="content">
      <el-tabs v-model="activeName" tab-position="left" style="height: 200px;">
        <el-tab-pane label="诉求提交" name="0">
          <div style="border:1px dotted #ddd;">
            <div style="margin-top:50px;margin-left:20px;width:600px;">
              <el-form :model="ruleForm" :rules="rules" ref="ruleFormRef" label-width="100px" class="demo-ruleForm">
                <el-form-item label="诉求类型" prop="orderType">
                  <el-select v-model="ruleForm.orderType" placeholder="请选择诉求类型">
                    <el-option label="资讯类" value="0"></el-option>
                    <el-option label="投诉类" value="1"></el-option>
                    <el-option label="举报类" value="2"></el-option>
                    <el-option label="求助类" value="3"></el-option>
                    <el-option label="建议类" value="4"></el-option>
                    <el-option label="其他" value="5"></el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="诉求人姓名" prop="appealer">
                  <el-input v-model="ruleForm.appealer"></el-input>
                </el-form-item>
                <el-form-item label="诉求人性别" prop="appealerSex">
                  <el-select v-model="ruleForm.appealerSex" placeholder="请选择诉求人性别">
                    <el-option label="男" value="1"></el-option>
                    <el-option label="女" value="0"></el-option>
                  </el-select>
                </el-form-item>

                <el-form-item label="诉求人电话" prop="telNumber">
                  <el-input v-model="ruleForm.telNumber"></el-input>
                </el-form-item>
                <el-form-item label="诉求主题" prop="title">
                  <el-input v-model="ruleForm.title"></el-input>
                </el-form-item>
                <el-form-item label="紧急程度" prop="appealerSex">
                  <el-select v-model="ruleForm.urgencyDegree" placeholder="请选择紧急程度">
                    <el-option label="一般" value="0"></el-option>
                    <el-option label="紧急" value="1"></el-option>
                    <el-option label="加急" value="2"></el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="诉求内容" prop="appealDetails">
                  <el-input type="textarea" v-model="ruleForm.appealDetails"></el-input>
                </el-form-item>
                <el-form-item>
                  <el-button type="primary" @click="submitCreat">立即创建</el-button>
                  <el-button @click="resetForm">重置</el-button>
                </el-form-item>
              </el-form>
            </div>
          </div>
        </el-tab-pane>
        <el-tab-pane label="诉求查询" name="1">
          <div style="height:300px;border:1px dotted #ddd;">
            <div style="margin-top:50px;margin-left:20px;width:500px;">
              <el-form :model="formQuery" status-icon ref="formQuery" label-width="100px" :rules="formRule">
                <el-form-item label="诉求人手机" prop="telNumber">
                  <el-input v-model.trim="formQuery.telNumber" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="诉求工单号">
                  <el-input v-model.trim="formQuery.orderNumber" autocomplete="off"></el-input>
                </el-form-item>

                <el-form-item>
                  <el-button type="primary" @click="submitQuery">提交</el-button>
                </el-form-item>
              </el-form>
            </div>
          </div>
        </el-tab-pane>
        <!-- <el-tab-pane label="知识库查询" name="2">
        <div style="height:500px;border:1px dotted #ddd;">
          <div style="margin-top:50px;margin-left:20px;width:600px;">
            <el-form
              :model="ruleForm2"
              status-icon
              ref="ruleForm"
              label-width="100px"
              class="demo-ruleForm"
            >
              <el-form-item label="要查询的内容">
                <el-input v-model="ruleForm2.pass" autocomplete="off"></el-input>
              </el-form-item>

              <el-form-item>
                <el-button type="primary" @click="submitForm2('ruleForm')">提交</el-button>
              </el-form-item>
            </el-form>
          </div>
        </div>
      </el-tab-pane> -->
        <div class="showquery">
          <el-table :data="queryList" style="width: 100%" v-if="queryList.length > 0" border @row-click="showAppeal">
            <el-table-column prop="appealer" label="姓名" width="120"></el-table-column>
            <el-table-column prop="orderNumber" label="诉求单号" width="180"></el-table-column>
            <el-table-column prop="orderStatusStr" label="诉求状态">
              <template slot-scope="scope">
                <span v-if="scope.row.orderStatusStr == 'ARCHIVED'">诉求已归档</span>
                <span v-if="scope.row.orderStatusStr == 'INITIATE'">诉求已发起成功</span>
                <span v-if="scope.row.orderStatusStr == 'ACCEPT' ">诉求已受理</span>
                <span v-if="scope.row.orderStatusStr == 'DISTRIBUTE'">诉求已派发</span>
                <span v-if="scope.row.orderStatusStr == 'DISPOSE'">诉求正在处理</span>
                <span v-if="scope.row.orderStatusStr == 'CONCLUDE'">诉求已办结</span>
                <span v-if="scope.row.orderStatusStr == 'VISIT' ">诉求正在回访</span>
                <span v-if="scope.row.orderStatusStr == 'PRETREATMENT' ">预处理</span>
              </template>
            </el-table-column>
            <el-table-column prop="title" label="诉求标题"></el-table-column>
          </el-table>
          <el-dialog :title="'诉求编号'+' '+queryDetail.orderNumber" :visible.sync="dialogVisible" center size='medium'>
            <div class="row">
              <!-- <div class="row-item clearfix">
              <span class="row-item-title">诉求工单编号：</span>
              <span class="span-input">{{queryDetail.orderNumber}}</span>
            </div> -->
              <div class="row-item clearfix">
                <span class="row-item-title">工单来源：</span>
                <span class="span-input">{{queryDetail.orderSource}}</span>
              </div>
              <div class="row-item clearfix">
                <span class="row-item-title">所属行政区：</span>
                <span class="span-input">吉州</span>
              </div>
              <div class="row-item clearfix">
                <span class="row-item-title">诉求主题：</span>
                <span class="span-input theme">{{queryDetail.title}}</span>
              </div>
              <div class="row-item clearfix">
                <span class="row-item-title">诉求内容：</span>
                <span class="span-input contents">{{queryDetail.appealDetails}}</span>
              </div>
              <div class="row-item clearfix">
                <span class="row-item-title">工单状态：</span>
                <span class="span-input">{{queryDetail.orderStatusStr}}</span>
              </div>
              <div class="row-item clearfix">
                <span class="row-item-title">工单创建时间：</span>
                <span class="span-input">{{queryDetail.createDate}}</span>
              </div>
              <div class="row-item clearfix">
                <span class="row-item-title">诉求人：</span>
                <span class="span-input">{{queryDetail.appealer}}</span>
              </div>
              <div class="row-item clearfix">
                <span class="row-item-title">问题分类：</span>
                <span class="span-input">{{queryDetail.orderType}}</span>
              </div>
              <div class="row-item clearfix" v-if="queryDetail.appealLocality">
                <span class="row-item-title">诉求发生地址：</span>
                <span class="span-input">{{queryDetail.urgencyDegree}}</span>
              </div>
            </div>
          </el-dialog>
        </div>
      </el-tabs>
    </div>
    <Footer />
  </div>
</template>
<script>
import { addOrder, queryOrder, queryFlowTelephone } from "@/api/user";
import Header from "~/components/Header.vue";
import Footer from "~/components/Footer.vue";

export default {
  components: { Header, Footer },
  data() {
    return {
      // 诉求提交对象
      ruleForm: {
        appealer: "",
        orderType: "",
        telNumber: "",
        title: "",
        appealerSex: "",
        urgencyDegree: "",
        beforeHand: "1",
        orderSource: "4",
      },
      // 诉求查询对象
      formQuery: {
        telNumber: "",
        orderNumber: "",
        orderSource: "4",
      },
      // 查询返回的工单数据
      queryList: [],
      ruleForm2: {
        pass: "",
      },
      rules: {
        appealer: [
          { required: true, message: "请输入诉求人姓名", trigger: "blur" },
        ],

        title: [
          { required: true, message: "请输入诉求主题", trigger: "blur" },
        ],
        urgencyDegree: [
          { required: true, message: "请选择紧急程度", trigger: "blur" },
        ],
        telNumber: [
          { required: true, message: "请输入诉求人电话", trigger: "blur" },
          { min: 11, max: 11, message: "长度在 11 个字符", trigger: "blur" },
        ],
        orderType: [
          { required: true, message: "请选择诉求类型", trigger: "change" },
        ],
        appealerSex: [
          { required: true, message: "请选择诉求人性别", trigger: "change" },
        ],
        date1: [
          {
            type: "date",
            required: true,
            message: "请选择日期",
            trigger: "change",
          },
        ],
        date2: [
          {
            type: "date",
            required: true,
            message: "请选择时间",
            trigger: "change",
          },
        ],
        type: [
          {
            type: "array",
            required: true,
            message: "请至少选择一个活动性质",
            trigger: "change",
          },
        ],
        appealDetails: [
          { required: true, message: "请填写诉求内容", trigger: "blur" },
          { max: 200, message: "长度超过200个字符", trigger: "blur" },
        ],
      },

      formRule: {
        telNumber: [
          { required: true, message: "请输入诉求人电话", trigger: "blur" },
          { min: 11, max: 11, message: "长度在 11 个字符", trigger: "blur" },
        ],
      },
      //选项卡的活动状态
      active: this.$route.query.active,
      activeName: "0",
      //诉求查询控制位
      queryDetail: {},
      dialogVisible: false,
    };
  },
  mounted() {
    console.log(this.active);
    if (this.active == true) {
      this.activeName = "1";
    }
  },
  methods: {
    //诉求提交
    submitCreat() {
      this.$refs["ruleFormRef"].validate(async (valid) => {
        if (valid) {
          let res = await addOrder(this.ruleForm);
          console.log(res);
          if (res && res.code == 0) {
            this.$message.success("诉求提交成功");

            this.$refs["ruleFormRef"].resetFields();
          } else if (res && res.code == 1) {
            this.$message.error("超出字数，请重新填写");
          } else {
            this.$message.error('数据异常，请稍后重试')
          }
        } else {
          this.$message.warning("请填写完整");
          return false;
        }
      });
    },
    //诉求查询
    async submitQuery() {
      if (this.formQuery.telNumber == "" && this.formQuery.orderNumber == "") {
        return this.$message.warning("手机号和工单号不能同时为空");
      } else {
        let res = await queryOrder(this.formQuery);
        console.log(res);

        if (res && res.code == 0) {
          this.queryList = res.data;
          console.log(this.queryList);
        }
      }
    },
    //重置
    resetForm() {
      this.$refs["ruleFormRef"].resetFields();
    },
    async showAppeal(row) {
      this.dialogVisible = true;
      let rowIndex = row.orderNumber;
      let params = { orderNum: rowIndex };
      let res = await queryFlowTelephone(params);
      this.queryDetail = res.data;
      this.queryDetail.createDate = this.formatDate(res.data.createDate);
      this.queryDetail.orderSource = this.switchSource(res.data.orderSource);
      console.log(row);
      this.queryDetail.orderStatusStr = this.switchStatus(res.data.orderStatusStr)
      this.queryDetail.orderType = this.swichType(res.data.orderType)
      // console.log(this.queryDetail)
    },
    //格式化工单详情时间
    formatDate(date) {
      var d = new Date(date),
        month = "" + (d.getMonth() + 1),
        day = "" + d.getDate(),
        year = d.getFullYear();

      if (month.length < 2) month = "0" + month;
      if (day.length < 2) day = "0" + day;

      return [year, month, day].join("-");
    },
    //处理来源
    switchSource(val) {
      let source = "";
      switch (val) {
        case 0:
          source = "电话";
          break;
        case 1:
          source = "微信";
          break;
        case 2:
          source = "微博";
          break;
        case 3:
          source = "APP";
          break;
        case 4:
          source = "网站";
          break;
        case 5:
          source = "邮件";
          break;
        case 6:
          source = "政务服务";
          break;
        case 7:
          source = "上级派发";
          break;
        case 8:
          source = "下级报送";
          break;
        default:
          source = "其他";
          break;
      }
      return source; status
    },
    //处理状态
    switchStatus(val) {
      let status = "";
      switch (val) {
        case 'INITIATE':
          status = "发起";
          break;
        case 'ACCEPT':
          status = "受理";
          break;
        case 'DISTRIBUTE':
          status = "派发";
          break;
        case 'DISPOSE':
          status = "处理";
          break;
        case 'CONCLUDE':
          status = "办结";
          break;
        case 'VISIT':
          status = "回访";
          break;
        case 'ARCHIVED':
          status = "归档";
          break;
        case 'PRETREATMENT':
          status = "预处理";
          break;
        default:
          status = "其他";
          break;
      }
      return status;
    },
    //处理问题分类
    swichType(val) {
      let type = "";
      switch (val) {
        case 0:
          type = "咨询";
          break;
        case 1:
          type = "投诉";
          break;
        case 2:
          type = "举报";
          break;
        case 3:
          type = "求助";
          break;
        case 4:
          type = "建议";
          break;
        case 5:
          type = "突发";
          break;
        default:
          type = "其他";
          break;
      }
      return type;
    },
  },
  head() {
    return {
      title: "12345概况-吉安12345政府服务热线",
      meta: [
        {
          hid: "吉安12345政府服务热线",
          name: "吉安12345政府服务热线",
          content: "吉安12345政府服务热线",
        },
      ],
    };
  },
};
</script>
<style scoped lang="scss">
.content_wrap {
  width: 100%;
  margin: 0 auto;
  .content {
    width: 1200px;
    margin: 20px auto;
  }
}

.el-tabs--left {
  height: 100% !important;
  /* height: 520px !important; */
}
.row-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 16px;
  padding: 10px 0;
  border-top: 1px solid #e6e6e6;
}
.row-item-title {
  color: #666;
  text-align: center;
}
>>> .el-dialog__title {
  font-size: 18px !important;
  letter-spacing: 2px;
}
>>> .el-dialog__body {
  max-height: 600px;
  overflow: auto;
  padding-top: 4px;
}
.span-input {
  max-width: 60%;
  text-align: right;
}
.span-input.contents,
.span-input.theme {
  text-align: left;
  text-indent: 32px;
}
</style>
